<?php

namespace App\Http\Controllers\admin;

use App\Http\Controllers\Controller;
use App\Models\LaborBlack;
use App\Services\LaborBlackService;
use Illuminate\Http\Request;

class LaborBlackController extends Controller
{
    protected $LaborBlackService;
    public function __construct(LaborBlackService $LaborBlackService)
    {
        $this->LaborBlackService = $LaborBlackService;
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $title = $request->title;
        $pageSize = $request->pageSize ?: 10;
        $page = $request->page ?: 1;
        $page = $page * $pageSize - $pageSize;

        $sql = $this->LaborBlackService->get($title);
        $sql->where('company_id',$request->company_id);
        $total = $sql->count();
        $data = $sql->skip($page)->take($pageSize)->orderBy('created_at','desc')->get();
        foreach ($data as $k => $v) {
            $data[$k]->name = $v->labor->name;
            $data[$k]->mobile = $v->labor->mobile;
            $data[$k]->photo = $v->labor->photo;
            $data[$k]->age = $v->labor->age;
            $data[$k]->sex = $v->labor->sex;
            $data[$k]->work = $v->labor->work;
            $data[$k]->address = $v->labor->address;
            $data[$k]->id_card = $v->labor->id_card;
            $data[$k]->home_address = $v->labor->home_address;
            $data[$k]->birth = $v->labor->birth;
            $data[$k]->nationality = $v->labor->nationality;
            $data[$k]->modality = $v->labor->modality;
            $data[$k]->emergency_contact = $v->labor->emergency_contact;
            $data[$k]->emergency_mobile = $v->labor->emergency_mobile;
        }
        return response()->json(['code' => 200, 'message' => '操作成功','page'=>$page,'pageSize'=>$pageSize,'total'=>$total,'data'=>$data]);
    }

    public function show(Request $request)
    {
        $data = LaborBlack::find($request->id);
        if($data->start_date){
            $data->start_date = strtotime($data->start_date) * 1000;
        }
        if($data->end_date){
            $data->end_date = strtotime($data->end_date) * 1000;
        }
        return response()->json(['code' => 200, 'message' => '操作成功','data'=>$data]);
    }


    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $POSTONLY = LaborBlack::POSTONLY;
        $input = $request->only($POSTONLY);
        $data = $this->LaborBlackService->insert($input);
        return response()->json(['code' => 200, 'message' => '操作成功','data'=>$data]);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function delete($id)
    {
        if(!$id){
            return response()->json(['code' => 404, 'message' => '请选择要操作的记录']);
        }
        $data = LaborBlack::find($id);
        $data->delete();
        return response()->json(['code' => 200, 'message' => '操作成功']);
    }
}
